package com.pearl.db;

import com.pearl.entitys.dataBaseTable.RolePermission;
import com.pearl.utils.Where;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RolePermissionDao {

  private static final String SQL_INS = "INSERT INTO role_permission(id,role_id,permission_id) VALUES (?,?,?)";

  private static final String SQL_UPD = "UPDATE role_permission SET role_id=?,permission_id=? WHERE id=?";

  private static final String SQL_SEL = "SELECT id,role_id,permission_id FROM role_permission ";

  private final Connection conn;

  public RolePermissionDao(Connection conn) {
    this.conn = conn;
  }

  public int insert(RolePermission data) {
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_INS)) {
      ps.setString(1, data.getId());
      ps.setString(2, data.getRoleId());
      ps.setString(3, data.getPermissionId());

      return ps.executeUpdate();
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public int insert(List<RolePermission> dataList){
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_INS)) {
     for(RolePermission data : dataList) {
      ps.setString(1, data.getId());
      ps.setString(2, data.getRoleId());
      ps.setString(3, data.getPermissionId());

      ps.addBatch();
     }
      return ps.executeBatch().length;
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public int update(RolePermission data){
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_UPD)) {
      ps.setString(1, data.getRoleId());
      ps.setString(2, data.getPermissionId());
      ps.setString(3, data.getId());

      return ps.executeUpdate();
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public int update(List<RolePermission> dataList){
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_UPD)) {
     for(RolePermission data : dataList) {
      ps.setString(1, data.getRoleId());
      ps.setString(2, data.getPermissionId());
      ps.setString(3, data.getId());

      ps.addBatch();
     }
     return ps.executeBatch().length;
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public int delete(String id){
    try (PreparedStatement ps = this.conn.prepareStatement("DELETE FROM role_permission WHERE id=?")) {
      ps.setString(1, id);

      return ps.executeUpdate();
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public List<RolePermission> selectAll(){
    ArrayList<RolePermission> result = new ArrayList<RolePermission>();
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_SEL)) {

      ResultSet rs = ps.executeQuery();
      while(rs.next()) {
        result.add(convert(rs));
      }
      return result;
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }


  public RolePermission selectByPK(String id){
    RolePermission result = null;
    try (PreparedStatement ps = this.conn.prepareStatement(SQL_SEL  + "WHERE id=?")) {
      ps.setString(1, id);


      ResultSet rs = ps.executeQuery();
      if(rs.next()) {
        result = convert(rs);
      }
      return result;
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  public List<RolePermission> selectWhere(Where where){
    List<RolePermission> result = new ArrayList<>();
    try (PreparedStatement ps = where.prepareStatement(conn, SQL_SEL)) {
      ResultSet rs = ps.executeQuery();
      while(rs.next()) {
        result.add(convert(rs));
      }
      return result;
    } catch (SQLException e) {
      throw new IllegalStateException("数据库查询错误, " + e.getMessage(), e);
    }
  }

  private RolePermission convert(ResultSet rs) throws SQLException {
    RolePermission data = new RolePermission();
    int index = 1;
    data.setId(rs.getString(index++));
    data.setRoleId(rs.getString(index++));
    data.setPermissionId(rs.getString(index++));

    return data;
  }
}
